	//扩展Date的format方法   
    Date.prototype.format = function (format) {  
        var o = {  
            "M+": this.getMonth() + 1,  
            "d+": this.getDate(),  
            "h+": this.getHours(),  
            "m+": this.getMinutes(),  
            "s+": this.getSeconds(),  
            "q+": Math.floor((this.getMonth() + 3) / 3),  
            "S": this.getMilliseconds()  
        }  
        if (/(y+)/.test(format)) {  
            format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));  
        }  
        for (var k in o) {  
            if (new RegExp("(" + k + ")").test(format)) {  
                format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));  
            }  
        }  
        return format;  
    }  
    /**   
    *转换日期对象为日期字符串   
    * @param date 日期对象   
    * @param isFull 是否为完整的日期数据,   
    *               为true时, 格式如"2000-03-05 01:05:04"   
    *               为false时, 格式如 "2000-03-05"   
    * @return 符合要求的日期字符串   
    */    
    function getSmpFormatDate(date, isFull) {  
        var pattern = "";  
        if (isFull == true || isFull == undefined) {  
            pattern = "yyyy-MM-dd hh:mm:ss";  
        } else {  
            pattern = "yyyy-MM-dd";  
        }  
        return getFormatDate(date, pattern);  
    }  
    /**   
    *转换当前日期对象为日期字符串   
    * @param date 日期对象   
    * @param isFull 是否为完整的日期数据,   
    *               为true时, 格式如"2000-03-05 01:05:04"   
    *               为false时, 格式如 "2000-03-05"   
    * @return 符合要求的日期字符串   
    */    
  
    function getSmpFormatNowDate(isFull) {  
        return getSmpFormatDate(new Date(), isFull);  
    }  
    /**   
    *转换long值为日期字符串   
    * @param l long值   
    * @param isFull 是否为完整的日期数据,   
    *               为true时, 格式如"2000-03-05 01:05:04"   
    *               为false时, 格式如 "2000-03-05"   
    * @return 符合要求的日期字符串   
    */    
  
    function getSmpFormatDateByLong(l, isFull) {  
        return getSmpFormatDate(new Date(l), isFull);  
    }  
    /**   
    *转换long值为日期字符串   
    * @param l long值   
    * @param pattern 格式字符串,例如：yyyy-MM-dd hh:mm:ss   
    * @return 符合要求的日期字符串   
    */    
  
    function getFormatDateByLong(l, pattern) {  
        return getFormatDate(new Date(l), pattern);  
    }  
    /**   
    *转换日期对象为日期字符串   
    * @param l long值   
    * @param pattern 格式字符串,例如：yyyy-MM-dd hh:mm:ss   
    * @return 符合要求的日期字符串   
    */    
    function getFormatDate(date, pattern) {  
        if (date == undefined) {  
            date = new Date();  
        }  
        if (pattern == undefined) {  
            pattern = "yyyy-MM-dd hh:mm:ss";  
        }  
        return date.format(pattern);  
    }  
    
   //long转date结束

   		function addInput(vote_title){
   			var li = '<li  class="vote_item"><div class="am-u-sm-9"><input type="text" class="vote_text" style="display:inline-block;width:440px;" value="';
			li += vote_title;
			li += '"/>&nbsp;<span class="del_btn">-</span>&nbsp;</div></li>';
			li =$(li);
			$("#vote_list").append(li);
			$("#vote_list").find(".del_btn").on("click",function(e){
				$(this).parent("").remove();
			});
   		}
   		
   		function addFirstInput(vote_title){
   			var li = '<li ><div class="am-u-sm-9"><input type="text" style="display:inline-block;width:440px;" value="';
			li += vote_title;
			li += '"/>&nbsp;<span class="add_btn">+</span>&nbsp;</div></li>';
			li =$(li);
			$("#vote_list").append(li);
			$("#vote_list").find(".add_btn").on("click",function(){
				add_vote();
			});
   		}
   		
		function add_vote(){
			var li = '<li class="vote_item"><div class="am-u-sm-9"><input type="text" class="vote_text" style="display:inline-block;width:440px;"/>&nbsp;<span class="del_btn">-</span>&nbsp;</div></li>';
			li =$(li);
			$("#vote_list").append(li);
			$("#vote_list").find(".del_btn").on("click",function(e){
				$(this).parent("").remove();
			});
		}
		
   		function initForm(aav){
   			var activity = aav.activity;
			var votes = aav.votes;
			var votes_count = votes.length;
			$("#title").val(activity.title);
			var vote_title;
			for(var k=0; k<votes_count; k++){
				vote_title = votes[k].title;
				if(k == 0){
					addFirstInput(vote_title);
				}else{
					addInput(vote_title);
				}
			}
			$("#begin_at").val(getSmpFormatDateByLong(activity.begin_at,false));
			$("#end_at").val(getSmpFormatDateByLong(activity.end_at,false));
			var state = activity.state;
			$("input:radio").eq(state).attr("checked",true);  
   		}

	 	$(function(){
		 	var id = getCookie("id");
		 	var activity_id = getCookie("activity_id");
		 	var activity_title = getCookie("activity_title");
		 	$.ajax({
				url:"http://localhost:8080/ciic-vote/activity/findByActivityId.do",
				type:"post",
				data:{"activityId":activity_id},
				dataType: "json",
				success:function(result){
					if(result.status == 0){
						var aav = result.data;
						initForm(aav);
					}
				}
			});
		 	
		 	
			$("#create_btn").click(function(){
				var lis = $("#vote_list").children();
				lis = $(lis);
				for(var i=0; i<lis.length; i++){
					var vote = lis.eq(i).children("input").val();
					var li = '<li><input type="hidden" value="';
					li += vote;
					li += '"></li>';
					li = $(li);
					$("#vote_list2").append(li);
				}

				resetName("#vote_list");

	 			$.ajax({
					url:"http://localhost:8080/ciic-vote/activity/modifyActivity.do",
					type:"post",
					data:$("#create_activity_form").serialize(),
					dataType: "json",
					success:function(result){
						if(result.status == 0){
							alert("修改成功");
							window.location.href="findAll_manager.html";
						}
					}
				}); 
			});

			$("#add_btn").click(function(){
				var li = '<li><div class="am-u-sm-9"><input type="text" class="vote_item"/>&nbsp;<span class="del_btn">-</span>&nbsp;</div></li>';
				li =$(li);
				$("#vote_list").append(li);
				$("#vote_list").find(".del_btn").on("click",function(e){
					$(this).parent("").remove();
				});
			});
			function resetName(iparent){
				iparent = $(iparent);
				iparent.find("input").each(function(index,element){
					element = $(element);
					var value = $(element).val();
					if (value)
						value = $.trim(value);
					if (value == null || value.length == 0) {
						element.parent().remove();
					} else {
						$(element).val(value);
					}
				});
			 	iparent.find("input").each(function(index, element) {
					element = $(element);
					element.attr("name", "votes["+index+"]");
				}); 
			}
	 	});